package com.pms.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pms.domain.Store;
import com.pms.service.StoreService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
@RequestMapping("/store")
public class StoreController {

	@Autowired
	private StoreService storeService;
	
	@RequestMapping(value = "/query.pfv",method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String getStores() throws IOException {
		List<Store> storeList = storeService.getAll();
		return new String(JSONArray.fromObject(storeList).toString().getBytes(),"iso8859-1");
	}

	@RequestMapping(value = "/getByStoreCode.pfv",method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String getStoreByStoreCode(@RequestParam(value="storeCode")String storeCode ) throws IOException {
		Store store = storeService.selectByStoreCode(storeCode) ;
		return new String(JSONObject.fromObject(store).toString().getBytes(),"utf-8");
	}
	
	@RequestMapping(value = "/insert.pfv",method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void insertStores(@RequestParam(value="store")Store store) {
		storeService.insertStore(store);
	}
	
	@RequestMapping(value = "/update.pfv",method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void updateStores(@RequestParam(value="store")Store store){
		storeService.updateByStoreCode(store);
	}
	
	@RequestMapping(value = "/delete.pfv",method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void deleteStores(@RequestParam(value="storeCode")String storeCode){
		storeService.deleteByStoreCode(storeCode);
	}
}

